package com.tkjt.pms.modules.tkindexinfo.service.impl;
import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tkjt.pms.common.entity.TkIndexInfo;
import com.tkjt.pms.common.utils.OrgGroupUtil;
import com.tkjt.pms.common.vos.TkIndexValueForScoreVO;
import com.tkjt.pms.modules.tkindexinfo.mapper.IndexInfoMapper;
import com.tkjt.pms.modules.tkindexinfo.service.IndexInfoService;
import com.tkjt.pms.modules.tkindexinfo.vo.TkIndexInfoVO;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: tk_index_info
 * @Author: jeecg-boot
 * @Date:   2020-02-14
 * @Version: V1.0
 */
@Service
public class IndexInfoServiceImpl implements IndexInfoService {
    @Autowired
    IndexInfoMapper indexInfoMapper;
    @Override
    public Result<?> list(TkIndexInfoVO tkIndexInfoVO) {
        int pageNo = tkIndexInfoVO.getPageNo();
        int pageSize = tkIndexInfoVO.getPageSize();
        Map<String, Object> queryIndexValue = new HashMap<>();
        queryIndexValue.put("min", (pageNo - 1) * pageSize);
        queryIndexValue.put("max", pageSize);
        if(StringUtils.isNotEmpty(tkIndexInfoVO.getIndexGroup())){
            String orgGroup = OrgGroupUtil.changeToCG(tkIndexInfoVO.getIndexGroup());
            queryIndexValue.put("indexGroup",orgGroup);
        }
        queryIndexValue.put("indexType",tkIndexInfoVO.getIndexType());
        queryIndexValue.put("indexCode",tkIndexInfoVO.getIndexCode());
        queryIndexValue.put("indexValueCode",tkIndexInfoVO.getIndexValueCode());
        queryIndexValue.put("indexName",tkIndexInfoVO.getIndexName());
        queryIndexValue.put("indexValueName",tkIndexInfoVO.getIndexValueName());
        List<TkIndexInfoVO> list = indexInfoMapper.selectPage(queryIndexValue);
        int count = indexInfoMapper.selectCount(queryIndexValue);
        Page<TkIndexInfoVO> finalPage = new Page<TkIndexInfoVO>(pageNo,pageSize);
        finalPage.setTotal(count);
        finalPage.setCurrent(pageNo);
        finalPage.setSize(pageSize);
        int pages = count / pageSize;
        if (count % pageSize > 0) {
            pages += 1;
        }
        finalPage.setPages(pages);
        finalPage.setRecords(list);
        return Result.ok(finalPage);
    }
}
